package com.test.reactor.mq.rocket;

import org.apache.rocketmq.client.producer.LocalTransactionState;
import org.apache.rocketmq.client.producer.TransactionListener;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.common.message.MessageConst;
import org.apache.rocketmq.common.message.MessageExt;
import org.springframework.stereotype.Service;

import lombok.extern.slf4j.Slf4j;

@Service
@Slf4j
public class TransactionCheckListenerImpl implements TransactionListener {

	@Override
	public LocalTransactionState checkLocalTransaction(MessageExt msg) {
		log.info("check--- " +  new String(msg.getBody()));
//		return LocalTransactionState.COMMIT_MESSAGE;
		return LocalTransactionState.UNKNOW;
	}

	@Override
	public LocalTransactionState executeLocalTransaction(Message message, Object arg1) {
		 log.info("msg = " + message.toString());
	        log.info("o = " + message.getProperty(MessageConst.PROPERTY_UNIQ_CLIENT_MESSAGE_ID_KEYIDX));
	        String tag = message.getTags();
	        if(tag.equals("test")) {
	            //这里有一个分阶段提交任务的概念
	        	log.info("这里处理业务逻辑，比如操作数据库，失败情况下进行ROLLBACK");
//	        	return LocalTransactionState.COMMIT_MESSAGE;
//	        	throw new RuntimeException();
	            return LocalTransactionState.UNKNOW;
	        }
	        log.info("COMMIT_MESSAGE");
	        return LocalTransactionState.COMMIT_MESSAGE;
	}

}
